package com.fz.us.admin.base.utils;


import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

/**
 * Created by dzw on 2018/3/29.
 */
public class ZipUtil {
    /**
     * 压缩文件
     * @param zipOutput ZipOutputStream
     * @param file 需要进行压缩的文件夹地址
     * @param base 压缩文件名字
     */
    public static void compressZip(ZipOutputStream zipOutput, File file, String base) throws IOException {

        if(file.isDirectory()){
            File[] listFiles = file.listFiles();// 列出所有的文件

            for(File fi : listFiles){
                if(fi.isDirectory()){
                    compressZip(zipOutput, fi, base + "/" + fi.getName());
                }else{
                    zip(zipOutput, fi, base);
                }
            }
        }else{
            zip(zipOutput, file, base);
        }
    }

    /**
     * 压缩的具体操作
     */
    public static void zip(ZipOutputStream zipOutput, File file, String base) throws IOException, FileNotFoundException {
        ZipEntry zEntry = new ZipEntry(base + File.separator + file.getName());
        zipOutput.putNextEntry(zEntry);

        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));

        byte[] buffer = new byte[1024];
        int read = 0;
        while((read =bis.read(buffer)) != -1){
            zipOutput.write(buffer, 0, read);
        }
        bis.close();
    }

    /**
     * 删除文件夹 （要调用这个）
     * @param folderPath
     */
    public static void delFolder(String folderPath) {
        try {
            delAllFile(folderPath); //删除完里面所有内容
            String filePath = folderPath;
            filePath = filePath.toString();
            java.io.File myFilePath = new java.io.File(filePath);
            myFilePath.delete(); //删除空文件夹
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除文件夹下的所有文件（不要调用这个，调上面那个）
     * @param path 文件夹的路径
     * @return
     */
    public static boolean delAllFile(String path) {
        boolean flag = false;
        File file = new File(path);
        if (!file.exists()) {
            return flag;
        }
        if (!file.isDirectory()) {
            return flag;
        }
        String[] tempList = file.list();
        File temp = null;
        for (int i = 0; i < tempList.length; i++) {
            if (path.endsWith(File.separator)) {
                temp = new File(path + tempList[i]);
            } else {
                temp = new File(path + File.separator + tempList[i]);
            }
            if (temp.isFile()) {
                temp.delete();
            }
            if (temp.isDirectory()) {
                delAllFile(path + "/" + tempList[i]);//先删除文件夹里面的文件
                delFolder(path + "/" + tempList[i]);//再删除空文件夹
                flag = true;
            }
        }
        return flag;
    }

    /**
     * 解压文件
     * @param inputStream 输入源zip输入流
     * @param outPath 解压到哪个目录下
     */
    public static void decompression(InputStream inputStream, String outPath){
        ZipInputStream Zin=new ZipInputStream(inputStream);
        BufferedInputStream Bin=new BufferedInputStream(Zin);
        File Fout=null;
        ZipEntry entry;
        try {
            while((entry = Zin.getNextEntry())!=null && !entry.isDirectory()){
                Fout=new File(outPath,entry.getName());
                if(!Fout.exists()){
                    (new File(Fout.getParent())).mkdirs();
                }
                FileOutputStream out=new FileOutputStream(Fout);
                BufferedOutputStream Bout=new BufferedOutputStream(out);
                int b;
                while((b=Bin.read())!=-1){
                    Bout.write(b);
                }
                Bout.close();
                out.close();
                System.out.println(Fout+"解压成功");
            }
            Bin.close();
            Zin.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

